import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
	
public class Congress {

	private String output = "blank.TerribadOUT";
	private OurPQ ourPQ;
	static BufferedReader br;
	int seats;
	private Member[] members;
	private static PrintStream outputStream = null;
	
	public static void main(String[] args) {
		new Congress(args[0]);
	}
	
	public Congress(String runConfig) {
		String tmp[] = runConfig.split("\\.");
		output = tmp[0]+".TerribadOUT";
		br = reader(runConfig,"UTF-8");
		createFile(output);
		outputStream = getOutputStream();
		Stopwatch sw = new Stopwatch();
		//due to convention we have Members and seats on the 2 first lines
		String fileInput = nextLine();
		String fileInputb = nextLine();
		members = new Member[Integer.parseInt(fileInput)];
		seats = Integer.parseInt(fileInputb);
		int x = 0;
		//While loop generates all members and adds a seat to each Member
		//aswell as storing them in an array for later use
		while(fileInput != null) {
			fileInput = nextLine();
			fileInputb = nextLine();
			if(fileInput == null || fileInputb == null) break;
			members[x] = new Member(fileInput,Integer.parseInt(fileInputb));
			members[x++].addSeat(); seats--;
		}
		double t = sw.elapsedTime(); //first time taking - read + generate
		ourPQ = new OurPQ(members);
		Member largest;
		while(seats > 0) { //distribution of seats till we reach 0
			largest = (Member) ourPQ.delMax();
			largest.addSeat(); seats--;
			ourPQ.insert(largest);
		}
		for(Member m : members) { //write output
			writeLineToFile(m.getName() + " " + m.getSeats());
		}
		double y = sw.elapsedTime(); //2nd time taking after output generation
		writeLineToFile("Seats Left after distribution: "+seats);
		writeLineToFile("Time to read from file and generate members: "+t);
		writeLineToFile("Time to appoint seats: \t"+(y-t));
		double u = sw.elapsedTime();
		writeLineToFile("Total time: \t"+u);
		writeLineToFile("<(*.*)> TerriBADS <(*.*<)");
	}
	
	
	/**
	 * Returns a PrintStream object tied to OUTPUT_FILE
	 * @return PrintStream that streams to OUTPUT_FILE file
	 */
	private PrintStream getOutputStream(){
		if(outputStream == null){
			createFile(output);
			try {
				outputStream = new PrintStream(output);
			} catch (IOException e1) {
			}
		}		
		return outputStream;
	}

	/**
	 * Create a file with filename. Note: if a file already exist with the same
	 * name it will be deleted.
	 * @param filename
	 * @return	The file object
	 */
	private File createFile(String filename){
		File file = new File(filename);
		if(file.exists())	file.delete();
		try {
			file.createNewFile();
		} catch (IOException e1) {
			System.out.println("File could not be created!");
			return null;
		}
		return file;
	}	
	
	public void writeLineToFile(String s){
		getOutputStream().println(s);
	}
	
	
	/**
	 * Return a BufferedReader object 
	 * @param filename of the file to read from
	 * @param encoding of the file's content
	 * @return BufferedReader object
	 */
	private BufferedReader reader(String filename, String encoding){
		BufferedReader br = null;
		try {
			br = new BufferedReader(
				new InputStreamReader(new FileInputStream(filename), encoding)
				);
		} catch (UnsupportedEncodingException e) {
		} catch (IOException e) {
		}
		return br;
	}
	
	/**
	 * Return the id, x, and y coordinates from the next line in the file
	 * @return	String[] id at [0], x-cord at [1] and y-cord at [2].
	 * Null if reaching end of file
	 */
	public String nextLine(){
		try {
			String s = br.readLine();
			if(s == null) return null;
			return s;
		} catch (IOException e) {
		}
		return null;
	}
}